package com.capgemini.expense.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.capgemini.expense.model.Employee;

public class EmployeeDao extends JPADao<Employee> {

	public EmployeeDao() {
		this.managedEntitiesType = Employee.class;
	}

	public void deleteAllEmployees() {
		EntityManager em = EntityManagerFactoryHolder.get().createEntityManager();
		try {
			Query q = em.createQuery("DELETE FROM Employee e");
			q.executeUpdate();
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("unchecked")
	public Employee findByEmailAndPassword(String email, String password) {
		EntityManager em = EntityManagerFactoryHolder.get().createEntityManager();
		try {
			Query q = em.createQuery("SELECT e FROM Employee e WHERE e.email = :email and e.password = :password");

			q.setParameter("email", email);
			q.setParameter("password", password);
			List<Employee> result = q.getResultList();
			if (result == null || result.size() == 0) {
				return null;
			} else {
				return (Employee) result.get(0);
			}
		} finally {
			em.close();
		}
	}

}
